package com.autologistics.socket.service;

import com.autologistics.dao.MachineDao;
import com.autologistics.model.Machine;
import com.autologistics.socket.vo.StateReportRes;

import net.sf.json.JSONObject;

public class ReportServices {

	private JSONObject reportJSON;
	
	
	public ReportServices(JSONObject reportJSON) {
		this.reportJSON = reportJSON;
	}


	public JSONObject processMachineReport(){
		
		int machineID = reportJSON.optInt("machineID",-1);
		StateReportRes response = new StateReportRes();
		if(SpringContextWait.beanFcBeanFactory == null){
			response.setResponseCode(4);
			response.setResponseDescription("system is starting ,please wait for a moment");
			return JSONObject.fromObject(response);
		}
		MachineDao machineDao = (MachineDao) SpringContextWait.beanFcBeanFactory.getBean("machineDao");
		

		
		int totalNumber = reportJSON.optInt("totalBlank",-1);
		int usedNumber = reportJSON.optInt("usedBlank",-1);
		int repairingNumber = reportJSON.optInt("repairingBlank",-1);
		int emptyNumber = reportJSON.optInt("emptyBlank",-1);
		int addressID = reportJSON.optInt("addressID", -1);
		
		if(totalNumber == -1 ||
				usedNumber == -1 ||
				repairingNumber == -1 ||
				emptyNumber == -1){
			response.setResponseCode(-2);
			response.setResponseDescription("all of  totalBlank,usedBlank,repairingBlank,emptyBlank is needed");
			return JSONObject.fromObject(response);
		}
		
		Machine machine = null ;
		if(machineID!= -1){
			machine= machineDao.getById(machineID);
		}
		if(machine == null && addressID != -1){
			machine= machineDao.getByAddressId(addressID);
		}
		
		if(machine == null){
			machine= machineDao.getByIDAddress(reportJSON.optString("machineip"));
		}
		
		if(totalNumber <0
				|| usedNumber<0
				|| repairingNumber<0
				|| emptyNumber<0){
			response.setResponseCode(4);
			response.setResponseDescription("all of totalBlank,usedBlank,repairingBlank,emptyBlank need to be positive");
			return JSONObject.fromObject(response);
		}

		if(machine == null){//register

			machine = new Machine();
			machine.setIpAddress(reportJSON.optString("machineip"));
			machine.setNumUsed(usedNumber);
			machine.setNumAll(totalNumber);
			machine.setNumRepair(repairingNumber);
			machineDao.add(machine);
			
			response.setResponseCode(5);
			response.setResponseDescription("warning server could not query this machine,server has auto register it,please complement it");
			return JSONObject.fromObject(response);
		}

		if(totalNumber !=
				(usedNumber + repairingNumber+ emptyNumber)){
			response.setResponseCode(6);
			response.setResponseDescription("totalNumber not equals (usedNumber + repairingNumber+ emptyNumber");
			return JSONObject.fromObject(response);
		}
		
		machine.setNumAll(totalNumber);
		machine.setNumRepair(repairingNumber);
		machine.setNumUsed(usedNumber);
		machineDao.updata(machine);
		response.setResponseCode(0);
		response.setResponseDescription("update sucessful");
		return JSONObject.fromObject(response);
	}
}
